/*************************************************
Author: tony
Date: 10/24/2012
Description:
Notes:
*************************************************/

#ifndef __CShopConfig_h__
#define __CShopConfig_h__

#include "commerrno.h"
#include "commclass.h"
#include "CTArray.h"
#include "../common/CRandom.h"

class CRoleToShopId
{
public:
    CRoleToShopId ()
    {
        _sRoleLevel = 0;
        _ucNearByShopId = 0;
        _ucMysticalShopId = 0;
        _ucClanShopId = 0;
        _iCaneCDTime = 0;
    }

    short _sRoleLevel;
    unsigned char _ucNearByShopId;
    unsigned char _ucMysticalShopId;    
    unsigned char _ucClanShopId;
    int  _iCaneCDTime;    
};

class CRoleToShopIdConfig
{
public:
    CRoleToShopIdConfig ()
    {
        _stRoleToShopId.m_iUsedCount = (MAX_ROLE_LEVEL + 1);
    }

    int AddConfig (CRoleToShopId & stConfigData)
    {
        int iIndex = stConfigData._sRoleLevel;
        if ((stConfigData._sRoleLevel < 1 || stConfigData._sRoleLevel > MAX_ROLE_LEVEL)
            || (0 != _stRoleToShopId[iIndex]._sRoleLevel))
        {
            return ERROR_SHOP_ROLE_TO_ID_CONFIG;
        }

        _stRoleToShopId[iIndex] = stConfigData;
        return 0;
    }

    int GetNearbyShopId (short sRoleLevel, unsigned char & ucNearByShopId)
    {
        if ((sRoleLevel < 1 || sRoleLevel > MAX_ROLE_LEVEL)
            || (0 == _stRoleToShopId[sRoleLevel]._sRoleLevel))
        {
            return ERROR_SHOP_ROLE_NOT_EXIST;
        }

        ucNearByShopId = _stRoleToShopId[sRoleLevel]._ucNearByShopId;
        return 0;
    }

    int GetMysticalShopId (short sRoleLevel, unsigned char & ucMysticalShopId)
    {
        if ((sRoleLevel < 1 || sRoleLevel > MAX_ROLE_LEVEL)
            || (0 == _stRoleToShopId[sRoleLevel]._sRoleLevel))
        {
            return ERROR_SHOP_ROLE_NOT_EXIST;
        }

        ucMysticalShopId = _stRoleToShopId[sRoleLevel]._ucMysticalShopId;
        return 0;
    }

    int GetClanShopId (short sRoleLevel, unsigned char & ucClanShopId)
    {
        if ((sRoleLevel < 1 || sRoleLevel > MAX_ROLE_LEVEL)
            || (0 == _stRoleToShopId[sRoleLevel]._sRoleLevel))
        {
            return ERROR_SHOP_ROLE_NOT_EXIST;
        }

        ucClanShopId = _stRoleToShopId[sRoleLevel]._ucClanShopId;
        return 0;
    }

    int GetCaneCdTime (short sRoleLevel, int & iCaneCDTime)
    {
        if ((sRoleLevel < 1 || sRoleLevel > MAX_ROLE_LEVEL)
            || (0 == _stRoleToShopId[sRoleLevel]._sRoleLevel))
        {
            return ERROR_SHOP_ROLE_NOT_EXIST;
        }

        iCaneCDTime = _stRoleToShopId[sRoleLevel]._iCaneCDTime;
        return 0;
    }

private:
    CTArray<CRoleToShopId, MAX_ROLE_LEVEL + 1> _stRoleToShopId;
};

class CShopItemConfig
{
public:
	CShopItemConfig()
    {
    	Clear();
	}

	void Clear()
	{
		_ucItemType = 0;
		_usItemConfigID = 0;
		_usItemCount = 0;
		_usDiscount = 0;
		_ucBuyType = 0;
		_ucExchangeItemType = 0;
		_usExchangeItemID = 0;
		_iExchangeItemCount = 0;
	}
#if 0
    void SetBuyFlag ()
    {
        _usExchangeItemID = SHOP_ITEM_SALE_OUT;
    }

    bool Has ()
    {
        return _usExchangeItemID = SHOP_ITEM_SALE_OUT;
    }
#endif
	virtual ~CShopItemConfig()
    {
	}

public:
	unsigned char _ucItemType;          //物品类型
    unsigned short _usItemConfigID;     //物品ID
    unsigned short _usItemCount;        //物品数量

    unsigned short _usDiscount;         //折扣，千分位
    unsigned char _ucBuyType;           //购买类型, 参考enum SHOP_BUY_TYPE

    unsigned char _ucExchangeItemType;          //兑换物品类型
    unsigned short _usExchangeItemID;           //兑换物品配置ID
    int  _iExchangeItemCount;        //兑换物品数量(_usItemCount物品的总数)

};

#define  MAX_ITEM_COUNT_OF_SHOP  50
typedef CTArray<CShopItemConfig, MAX_ITEM_COUNT_OF_SHOP> T_SHOP_ITEM_LIST;

//-------------------------------随身商店配置(BEGIN) --------------------------------

//商店里可出售的不同商品类型最大数目
#define MAX_NEAR_BY_SHOP_ITEM_NUM   30
class CNearByShopConfigUnit
{
public:
    CNearByShopConfigUnit ()
    {
        _stItemList.Clear();
    }

    int AddShopItem (CShopItemConfig& stConfigData)
    {
        if (_stItemList.Size() >= MAX_NEAR_BY_SHOP_ITEM_NUM)
        {
            return ERROR_SHOP_NEARBY_CONFIG;
        }

        _stItemList.AddOneItem(stConfigData);
        return 0;
    }

    int GetShopItem (unsigned char ucPos, CShopItemConfig& stConfigData)
    {
        if (ucPos > _stItemList.Size())
        {
            return ERROR_SHOP_NEARBY_NOT_EXIST;
        }

        stConfigData = _stItemList[ucPos];
        return 0;
    }

    CTArray<CShopItemConfig, MAX_NEAR_BY_SHOP_ITEM_NUM> _stItemList;
};

//随身商店的个数
#define MAX_NEARBY_SHOP_COUNT  40
class CNearByShopConfig
{
public:
	CNearByShopConfig()
    {
        _astNearByShopConfig.m_iUsedCount = MAX_NEARBY_SHOP_COUNT;
	}

	virtual ~CNearByShopConfig()
    {
	}

    int AddConfigData(unsigned char ucNearByShopId, CShopItemConfig& stConfigData)
    {
        if (ucNearByShopId >= MAX_NEARBY_SHOP_COUNT)
        {
            return ERROR_SHOP_CONFIGID_ERR;
        }

        _astNearByShopConfig[ucNearByShopId].AddShopItem(stConfigData);
    	return 0;
    }

    int GetConfigData(unsigned char ucNearByShopId, T_SHOP_ITEM_LIST & astItemList)
    {
        if (ucNearByShopId >= MAX_NEARBY_SHOP_COUNT)
        {
            return ERROR_SHOP_CONFIGID_ERR;
        }

        astItemList.Clear();
        for (int i = 0; i < _astNearByShopConfig[ucNearByShopId]._stItemList.Size(); i++)
        {
            astItemList.AddOneItem(_astNearByShopConfig[ucNearByShopId]._stItemList[i]);
        }

    	return 0;
    }

    int GetConfigData(unsigned char ucNearByShopId, unsigned char ucPos, CShopItemConfig& stConfigData)
    {
        if (ucNearByShopId >= MAX_NEARBY_SHOP_COUNT)
        {
            return ERROR_SHOP_CONFIGID_ERR;
        }

        return _astNearByShopConfig[ucNearByShopId].GetShopItem(ucPos, stConfigData);
    }
private:
     CTArray<CNearByShopConfigUnit, MAX_NEARBY_SHOP_COUNT> _astNearByShopConfig;
};
//-------------------------------随身商店配置(END) ---------------------------------

//-------------------------------装备商店配置(BEGIN) --------------------------------

//装备商店
#define MAX_EQUIP_SHOP_CONFIG_COUNT  20
class CEquipShopConfig
{
public:
    CEquipShopConfig ()
    {
        _stItemList.Clear();
    }

    int AddConfigData(CShopItemConfig& stConfigData)
    {
        if (_stItemList.Size() >= MAX_EQUIP_SHOP_CONFIG_COUNT)
        {
            return ERROR_EQUIP_SHOP_CONFIG;
        }

        _stItemList.AddOneItem(stConfigData);
        return 0;
    }

    int GetConfigData(T_SHOP_ITEM_LIST & astItemList)
    {
        astItemList.Clear();
        for (int i = 0; i < _stItemList.Size(); i++)
        {
            astItemList.AddOneItem(_stItemList[i]);
        }

        return 0;
    }

    int GetConfigData(unsigned char ucPos, CShopItemConfig& stConfigData)
    {
        if (ucPos >= MAX_EQUIP_SHOP_CONFIG_COUNT)
        {
            return ERROR_EQUIP_SHOP_CONFIG;
        }

        stConfigData = _stItemList[ucPos];
        return 0;
    }

private:
    CTArray<CShopItemConfig, MAX_EQUIP_SHOP_CONFIG_COUNT> _stItemList;
};

//-------------------------------装备商店配置(END) ---------------------------------

//-------------------------------神秘商店配置(BEGIN) ---------------------------------

//刷新物品的方式
enum EN_MYSTICAL_UPDATE_TYPE
{
    MYSTICAL_UPDATE_CANE = 1,
    MYSTICAL_UPDATE_GOLD = 2,
};

#define SHOP_ITEM_CFG_SUBTYPE_STR_LEN  50    //物品子类型字符串长度
class CMysticalShopItemConfig
{
public:
    typedef CTSizeString<SHOP_ITEM_CFG_SUBTYPE_STR_LEN> T_ITEM_SUBTYPE_CONFIG_KEY;
    CMysticalShopItemConfig()
    {

    }

public:
    unsigned char                _ucShopItemType;
    unsigned char                _ucShopCount;
    unsigned short               _usConfigId;
    T_ITEM_SUBTYPE_CONFIG_KEY    _szShopItemSubType;

    //普通\金币刷新概率存放在CMysticalShopItemDrop中
};

#define MAX_MYSTICAL_SHOP_ITEM_NUM     40    //每个商店最多可配置物品
class CMysticalShopConfigUnit
{
public:
    CMysticalShopConfigUnit()
    {
        Clear();
    }

    void Clear()
    {
        _stItemList.Clear();
    }

    int AddShopItem (CMysticalShopItemConfig& stConfigData, unsigned char & ucPos)
    {
        if (_stItemList.Size() >= MAX_MYSTICAL_SHOP_ITEM_NUM)
        {
            return ERROR_SHOP_MYSTICAL_CONFIG;
        }

        ucPos = _stItemList.AddOneItem(stConfigData);
        return 0;
    }

    int GetShopItem (unsigned char ucPos, CMysticalShopItemConfig& stConfigData)
    {
        if (ucPos > _stItemList.Size())
        {
            return ERROR_SHOP_MYSTICAL_NOT_EXIST;
        }

        stConfigData = _stItemList[ucPos];
        return 0;
    }

private:
    CTArray<CMysticalShopItemConfig, MAX_MYSTICAL_SHOP_ITEM_NUM> _stItemList;
};

#define MAX_MYSTICAL_SHOP_COUNT   40    //神秘商店最多个数
class CMysticalShopItemDrop
{
public:
    CMysticalShopItemDrop()
    {
        _astNormDrop.Clear();
        _astGoldDrop.Clear();
    };

    //ucPos存放掉落物品在CMysticalShopConfigUnit中数组的下标
    int AddDropData(unsigned char ucShopId, unsigned char ucPos, unsigned char ucDropType, int iDropRate);

    int DropShopItem (unsigned char ucShopId ,unsigned char ucDropType, unsigned char &ucPos);

    typedef CRandomSet<MAX_MYSTICAL_SHOP_ITEM_NUM> T_MYSTICAL_SHOP_DROP;  //每个商店可以销售的物品
    typedef CTLib::CTArray<T_MYSTICAL_SHOP_DROP, MAX_MYSTICAL_SHOP_COUNT> T_MYSTICAL_SHOP_SET;

public:
    T_MYSTICAL_SHOP_SET _astNormDrop;
    T_MYSTICAL_SHOP_SET _astGoldDrop;
};

class CMysticalShopConfig
{
public:

    CMysticalShopConfig()
    {
    }

    virtual ~CMysticalShopConfig()
    {
    }

    int CountSize()
    {
        return _astMystShopConfig.Size();
    }

    int AddConfigData(unsigned char ucMysticalShopId, CMysticalShopItemConfig & stConfigData, unsigned char & ucPos)
    {
        if (ucMysticalShopId >= MAX_MYSTICAL_SHOP_COUNT)
        {
            return ERROR_SHOP_MYSTICAL_NOT_EXIST;
        }

        return _astMystShopConfig[ucMysticalShopId].AddShopItem (stConfigData, ucPos);
    }

    int GetConfigData(unsigned char ucMysticalShopId, CMysticalShopConfigUnit & stConfigData)
    {
        if (ucMysticalShopId >= MAX_MYSTICAL_SHOP_COUNT)
        {
            return ERROR_SHOP_MYSTICAL_NOT_EXIST;
        }

        stConfigData = _astMystShopConfig[ucMysticalShopId];

        return 0;
    }

public:
    CTArray<CMysticalShopConfigUnit, MAX_MYSTICAL_SHOP_COUNT> _astMystShopConfig;
    CMysticalShopItemDrop _stMystItemDrop;
};

//-------------------------------神秘商店配置(END) ---------------------------------

//-------------------------------家族商店配置(BEGIN) ---------------------------------

class CClanShopItemConfig
{
public:
    typedef CTSizeString<SHOP_ITEM_CFG_SUBTYPE_STR_LEN> T_ITEM_SUBTYPE_CONFIG_KEY;
    CClanShopItemConfig()
    {

    }

public:
    unsigned char                _ucShopItemType;
    unsigned char                _ucShopCount;
    unsigned short               _usItemConfigId;
    T_ITEM_SUBTYPE_CONFIG_KEY    _szShopItemSubType;
};

#define MAX_CLAN_SHOP_COUNT   40    //家族商店ID的最多个数
class CClanShopConfigUnit
{
public:
    CClanShopConfigUnit()
    {
        Clear();
    }

    void Clear()
    {
        _stItemList.Clear();
    }

    int AddShopItem (CClanShopItemConfig& stConfigData, unsigned char & ucPos)
    {
        if (_stItemList.Size() > MAX_CLAN_SHOP_ITEM)
        {
            CT_ERROR(("%d", _stItemList.Size()));
            return ERROR_SHOP_CLAN_CONFIG;
        }

        ucPos = _stItemList.AddOneItem(stConfigData);
        return 0;
    }

    int GetShopItem (unsigned char ucPos, CClanShopItemConfig& stConfigData)
    {
        if (ucPos > _stItemList.Size())
        {            
            CT_ERROR(("%d, %d", ucPos, _stItemList.Size()));
            return ERROR_SHOP_CLAN_CONFIG;
        }

        stConfigData = _stItemList[ucPos];
        return 0;
    }

private:
    CTArray<CClanShopItemConfig, MAX_CLAN_SHOP_ITEM> _stItemList;
};

class CClanShopItemDrop
{
public:
    CClanShopItemDrop()
    {
    };

    //ucPos存放掉落物品在CMysticalShopConfigUnit中数组的下标
    int AddDropData(unsigned char ucShopId, unsigned char ucPos, int iDropRate);

    int DropShopItem (unsigned char ucShopId, unsigned char &ucPos);

    typedef CRandomSet<MAX_CLAN_SHOP_ITEM> T_CLAN_SHOP_DROP;  //每个商店可以销售的物品
    typedef CTLib::CTArray<T_CLAN_SHOP_DROP, MAX_CLAN_SHOP_COUNT> T_CLAN_SHOP_SET;

public:
    T_CLAN_SHOP_SET _astDrop;
};

class CClanShopConfig
{
public:

    CClanShopConfig()
    {
    }

    virtual ~CClanShopConfig()
    {
    }

    int CountSize()
    {
        return _astClanShopConfig.Size();
    }

    int AddConfigData(unsigned char ucClanShopId, CClanShopItemConfig & stConfigData, unsigned char & ucPos)
    {        
        return _astClanShopConfig[ucClanShopId].AddShopItem (stConfigData, ucPos);
    }

    int GetConfigData(unsigned char ucClanShopId, CClanShopConfigUnit & stConfigData)
    {
        if (ucClanShopId >= MAX_CLAN_SHOP_COUNT)
        {
            return ERROR_SHOP_CLAN_NOT_EXIST;
        }

        stConfigData = _astClanShopConfig[ucClanShopId];

        return 0;
    }

public:
    CTArray<CClanShopConfigUnit, MAX_CLAN_SHOP_COUNT> _astClanShopConfig;
    CClanShopItemDrop _stClanItemDrop;
};

//-------------------------------家族商店配置(END) ---------------------------------

//-------------------------------宝石商店配置(BEGIN) ---------------------------------
class CCrystalShopItemConfig
{
public:
    CCrystalShopItemConfig() : _ucItemType(0), _usItemId(0), _iItemCount(0), _iSealCost(0), _iGoldCost(0)
    {
    }

public:
    unsigned char                _ucItemType;
    unsigned short               _usItemId;
    int   _iItemCount; 
    int   _iSealCost;            //印章数
    int   _iGoldCost;            //金币数
};

class CCrystalShopConfig
{
public:

    CCrystalShopConfig()
    {
    }

    virtual ~CCrystalShopConfig()
    {
    }

    int AddConfigData(CCrystalShopItemConfig & stConfigData)
    {
        if (_astConfig.Size() >= MAX_CRYSTAL_SHOP_COUNT)
        {
            CT_ERROR(("%d", _astConfig.Size()));
            return ERROR_CRYSTAL_SHOP_CONFIG;
        }

        _astConfig.AddOneItem(stConfigData);
        return 0;
    }

    int GetConfigData(unsigned short usPos, CCrystalShopItemConfig & stConfigData)
    {
        if (usPos >= MAX_CRYSTAL_SHOP_COUNT)
        {
            CT_ERROR(("%d, %d", usPos, _astConfig.Size()));
            return ERROR_CRYSTAL_SHOPCFG_NONE;
        }

        stConfigData = _astConfig[usPos];

        return 0;
    }

public:
    CTArray<CCrystalShopItemConfig, MAX_CRYSTAL_SHOP_COUNT> _astConfig;    
};
//-------------------------------宝石商店配置(END) ---------------------------------


//-------------------------------坐骑商店配置(BEGIN) ---------------------------------
class CMountShopItemConfig
{
public:
    CMountShopItemConfig()
    {
        Clear();
    }

    void Clear()
    {
        usMountId = 0;
        usMaterialId = 0;
        iSilver = 0;
    }

    unsigned short usMountId;
    int iSilver;
    unsigned short usMaterialId;
};

class CMountShopUnitConfig
{
public:
    CMountShopUnitConfig ()
    {
        ucShopId = 0;
        ucLevel = 0;
        stItem1.Clear();
        stItem2.Clear();
    }

    unsigned char ucShopId;
    unsigned char ucLevel;  //开放等级
    CMountShopItemConfig stItem1;
    CMountShopItemConfig stItem2;
};

#define  MAX_MOUNT_SHOP_COUNT   10
class CMountShopConfig
{
public:
    CMountShopConfig()
    {
        _stCfg.m_iUsedCount = MAX_MOUNT_SHOP_COUNT;
    }

    int AddMountItem(unsigned char ucShopId, unsigned char ucLevel, CMountShopItemConfig & stItem)
    {
        return 0;
    }

    CTArray<CMountShopUnitConfig, MAX_MOUNT_SHOP_COUNT> _stCfg;
};

//-------------------------------坐骑商店配置(END) ---------------------------------

//-------------------------------时装商店配置(BEGIN) ---------------------------------
//-------------------------------时装商店配置(END) ---------------------------------


#endif // __CShopConfig_h__
